/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;


/**
 * The FileSystem implementation of ConfigLoader
 * 
 * @author Laurent Guerin
 * @since 1.2.0
 *
 */
public class FileSystemConfigLoader extends GenericConfigLoader implements ConfigLoader
{
	private String sTelosysPropertiesFileName = null ;
	
    //------------------------------------------------------------------------------------------------
	/* (non-Javadoc)
	 * @see org.objectweb.telosys.common.config.ConfigLoader#getOrigin()
	 */
	public String getOrigin() {
		return "File System" ;
	}

    //------------------------------------------------------------------------------------------------
	/**
	 * Constructor 
	 * @param filePath  the file to be loaded
	 */
	public FileSystemConfigLoader(String filePath) {
		super();
		sTelosysPropertiesFileName = filePath ; 
	}
	
    //------------------------------------------------------------------------------------------------
	/* (non-Javadoc)
	 * @see org.objectweb.telosys.common.config.ConfigLoader#loadTelosysProperties()
	 */
	public TelosysConfig loadTelosysProperties() throws Exception
	{
		if ( sTelosysPropertiesFileName != null ) {
			return loadPropertiesFromFileSystem ( sTelosysPropertiesFileName );
		}
		else {
			throw new Exception("Cannot load properties (file path is null)");
		}
	}
	
    //------------------------------------------------------------------------------------------------
	/* (non-Javadoc)
	 * @see org.objectweb.telosys.common.config.ConfigLoader#getInputStream(java.lang.String)
	 */
	public InputStream getInputStream ( String sFileName ) throws Exception
    {
        FileInputStream fis = null ;
        try
        {
            fis = new FileInputStream(sFileName);
        } catch (FileNotFoundException e)
        {
        	throw new Exception("Cannot create FileInputStream : file '" + sFileName + "' not found " );
        }
        return fis;
    }
	
    //------------------------------------------------------------------------------------------------
    /**
     * Loads a properties file located in the file system
     * @param sFileName
     * @return configuration instance
     */
    private TelosysConfig loadPropertiesFromFileSystem ( String sFileName ) throws Exception
    {
        TelosysConfig tp = null ;
        FileInputStream fis = null ;
        try
        {
            fis = new FileInputStream(sFileName);
        } catch (FileNotFoundException e)
        {
			throw new Exception("Cannot load properties : file '" + sFileName + "' not found " );
        }
        Properties prop = loadPropertiesInputStream(fis);
        tp = buildFileSystemProperties ( prop, sFileName );
        return tp;
    }

    //------------------------------------------------------------------------------------------------
    /**
     * @param prop
     * @param sFilePath
     * @return
     */
    private TelosysConfig buildFileSystemProperties ( Properties prop, String sFilePath )
    {
        if ( prop != null && sFilePath != null )
        {
            //return new TelosysProperties(prop, sFilePath, TelosysProperties.FILESYSTEM );
            return new TelosysConfig(prop, sFilePath, this );
        }
        return null ;
    }    

}
